Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

16.3 Early Tracing Framework Configuration

early_trace_init() is a critical initialization function in the Linux kernel boot sequence. It is responsible for setting up a simplified, rudimentary tracing mechanism before the comprehensive tracing subsystem has completed its full initialization, thereby allowing the system to log diagnostics during the earliest phases of the boot process. The function is defined within the source file git/kernel/trace/trace.c, and its definition is implemented as follows:

void __init early_trace_init(void)
{
	if (tracepoint_printk) {
		tracepoint_print_iter =  kmalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
		if (MEM_FAIL(!tracepoint_print_iter,  "Failed to allocate trace iterator\n"))
			tracepoint_printk = 0;
		else
			static_key_enable(&tracepoint_printk_key.key);
	}
	tracer_alloc_buffers();
}

Its primary technical functionalities encompass:

  • If tracepoint_printk is enabled, the routine leverages kmalloc() to allocate memory from the kmalloc slabs to instantiate a tracepoint_print_iter structure to track the log printing iteration state. Concurrently, it leverages static jump label patching technology to activate early logging printouts. The tracepoint_printk feature is toggled via the kernel boot parameter tp_printk. When active, it allows all message payloads emitted by kernel tracepoints to be redirected straight into the primary kernel log ring buffer (printk / dmesg).

  • It invokes the function tracer_alloc_buffers() to allocate the primary ring buffers for trace logging, bootstrap the per-CPU lookaside tracing areas, and establish the core storage canvas for tracing data. For a comprehensive structural overview of log ring buffer mechanics, readers can refer to Chapter 14.

Beyond executing raw memory allocations, tracer_alloc_buffers() constructs the minimal operational runtime environment required for kernel tracing. Its principal operations encompass:

  1. It first queries security_locked_down(LOCKDOWN_TRACEFS) to verify whether the operating system resides under a strict kernel lockdown modality. If the kernel has been locked down for security hardening, the tracing infrastructure is disabled to prevent sensitive information disclosure, and the routine returns a permissions error.

  2. It invokes alloc_cpumask_var() to allocate a CPU bitmask to determine which individual CPU cores are authorized to participate in tracing passes. It checks whether trace_printk() is actively called inside the kernel; if detected, it initializes dedicated context buffers to ensure debugging outputs can be recorded from any execution context.

  3. If a prior ring buffer expansion pass was attempted and evaluated as successful, the system maps trace_buf_size to dictate the baseline value of ring_buf_size; otherwise, it forces ring_buf_size to scale down to its minimum permissible threshold.

  4. It calls cpuhp_setup_state_multi() to register CPU hotplug handlers. Consequently, when a new CPU core is dynamically brought online at runtime, the kernel automatically carves out a matching local ring buffer allocation for that core, guaranteeing cross-core tracing continuity in symmetric multiprocessing environments.

  5. It allocates a temporary ring buffer staging slice (scaled to a single memory page frame) to facilitate specialized workloads such as event triggers, while concurrently invoking allocate_trace_buffers() to formally allocate the core ring buffer channels for the global tracing instance global_trace.

  6. If the kernel boot parameter trace_boot_clock has been specified, it configures the tracing timebase source by evaluating tracing_set_clock().

  7. It populates the individual fields of the global_trace master structure to prepare for registering the primary tracking engines via register_tracer().

  8. It invokes init_function_trace() to execute the initial setup passes for the core function tracing engine.

  9. It registers custom tracing callback handlers onto the kernel's global notification chains panic_notifier_list and die_notifier. This architecture ensures that if a kernel panic or a fatal CPU exception manifests, the tracing subsystem can immediately trigger a callback routine to execute safety logic, such as preserving a localized trace snapshot in memory.

  10. It initializes the diverse tracking list heads embedded inside the global_trace structure and appends this master node into the global trace array index registry ftrace_trace_arrays.

  11. It executes apply_trace_boot_options() to enforce the specific tracing configuration flags specified during the system boot sequence.

  12. It registers the snapshot event triggers, concluding the early tracking framework initialization pipeline.

Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com